package test02;

// 命令接口
interface Command {
    void execute();
}

// 灯具类（接收者）
class Light {
    private String location;

    public Light(String location) {
        this.location = location;
    }

    public void on() {
        System.out.println(location + " 灯打开了");
    }

    public void off() {
        System.out.println(location + " 灯关闭了");
    }
}

// 开灯命令
class LightOnCommand implements Command {
    private Light light;

    public LightOnCommand(Light light) {
        this.light = light;
    }

    @Override
    public void execute() {
        light.on();
    }
}

// 关灯命令
class LightOffCommand implements Command {
    private Light light;

    public LightOffCommand(Light light) {
        this.light = light;
    }

    @Override
    public void execute() {
        light.off();
    }
}

// 空命令（用于初始化插槽）
class NoCommand implements Command {
    @Override
    public void execute() {
        System.out.println("该插槽未绑定任何灯具");
    }
}

// 遥控器（调用者）
class RemoteControl {
    private Command[] onCommands;
    private Command[] offCommands;

    public RemoteControl() {
        onCommands = new Command[7];
        offCommands = new Command[7];

        // 初始化所有插槽为空命令
        Command noCommand = new NoCommand();
        for (int i = 0; i < 7; i++) {
            onCommands[i] = noCommand;
            offCommands[i] = noCommand;
        }
    }

    // 设置插槽命令
    public void setCommand(int slot, Command onCommand, Command offCommand) {
        if (slot < 0 || slot >= 7) {
            System.out.println("无效的插槽编号，请输入0-6之间的数字");
            return;
        }

        onCommands[slot] = onCommand;
        offCommands[slot] = offCommand;
        System.out.println("插槽 " + slot + " 已绑定灯具");
    }

    // 按下开按钮
    public void onButtonPressed(int slot) {
        if (slot < 0 || slot >= 7) {
            System.out.println("无效的插槽编号");
            return;
        }
        System.out.print("按下插槽 " + slot + " 的开按钮: ");
        onCommands[slot].execute();
    }

    // 按下关按钮
    public void offButtonPressed(int slot) {
        if (slot < 0 || slot >= 7) {
            System.out.println("无效的插槽编号");
            return;
        }
        System.out.print("按下插槽 " + slot + " 的关按钮: ");
        offCommands[slot].execute();
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("\n------ 遥控器配置 ------\n");
        for (int i = 0; i < 7; i++) {
            stringBuilder.append("[插槽 ").append(i).append("] ")
                    .append(onCommands[i].getClass().getSimpleName())
                    .append("    ")
                    .append(offCommands[i].getClass().getSimpleName())
                    .append("\n");
        }
        return stringBuilder.toString();
    }
}

// 演示类
public class SmartRemoteControlDemo {
    public static void main(String[] args) {
        // 创建遥控器
        RemoteControl remoteControl = new RemoteControl();

        // 创建不同类型的灯具
        Light livingRoomLight = new Light("客厅");
        Light kitchenLight = new Light("厨房");
        Light bedroomLight = new Light("卧室");
        Light bathroomLight = new Light("卫生间");
        Light studyRoomLight = new Light("书房");

        // 创建命令对象
        LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight);
        LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);

        LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight);
        LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight);

        LightOnCommand bedroomLightOn = new LightOnCommand(bedroomLight);
        LightOffCommand bedroomLightOff = new LightOffCommand(bedroomLight);

        LightOnCommand bathroomLightOn = new LightOnCommand(bathroomLight);
        LightOffCommand bathroomLightOff = new LightOffCommand(bathroomLight);

        LightOnCommand studyRoomLightOn = new LightOnCommand(studyRoomLight);
        LightOffCommand studyRoomLightOff = new LightOffCommand(studyRoomLight);

        // 绑定灯具到遥控器插槽
        remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
        remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);
        remoteControl.setCommand(2, bedroomLightOn, bedroomLightOff);
        remoteControl.setCommand(3, bathroomLightOn, bathroomLightOff);
        remoteControl.setCommand(4, studyRoomLightOn, studyRoomLightOff);

        // 显示遥控器配置
        System.out.println(remoteControl);

        // 演示遥控器使用
        System.out.println("=== 遥控器操作演示 ===");

        // 打开各个房间的灯
        remoteControl.onButtonPressed(0); // 打开客厅灯
        remoteControl.onButtonPressed(1); // 打开厨房灯
        remoteControl.onButtonPressed(2); // 打开卧室灯

        System.out.println();

        // 关闭部分灯
        remoteControl.offButtonPressed(1); // 关闭厨房灯
        remoteControl.offButtonPressed(2); // 关闭卧室灯

        System.out.println();

        // 尝试操作未绑定的插槽
        remoteControl.onButtonPressed(5); // 插槽5未绑定
        remoteControl.offButtonPressed(6); // 插槽6未绑定

        System.out.println();

        // 演示重新绑定插槽
        System.out.println("=== 重新绑定插槽 ===");
        Light gardenLight = new Light("花园");
        LightOnCommand gardenLightOn = new LightOnCommand(gardenLight);
        LightOffCommand gardenLightOff = new LightOffCommand(gardenLight);

        remoteControl.setCommand(5, gardenLightOn, gardenLightOff);
        remoteControl.onButtonPressed(5); // 现在可以打开花园灯
    }
}
